/**
 * Copyright ©2003~2019年7月31日 szkingdom
 * @author raohj
 * @date 2019年7月31日
 * @file RResources.java
 * @comment : please input your comment to this file
 * @version : input version
 * @modify:
 * 		1、 2019年7月31日 由 raohj 新建文件
 */
package com.ccnu.demojava.spring.framework.resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.Resource;

import java.io.IOException;
import java.util.Enumeration;
import java.util.Properties;

/**
 * @author raohj
 * @date 2019年7月31日
 * @version
 * @comment to add your comment
 */
public class RResources {
	private static final Logger log = LoggerFactory.getLogger(RResources.class);

	public static void test() {
		testClassPathResource();
		testFileSystemResource();
	}

	public static void testUrlResource() {

	}

	public static void testClassPathResource() {
		log.info("----------测试testClassPathResource函数---------------------");
		ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext();
		Resource rc = ctx.getResource("config-local.properties"); // 默认会去查找classpath下面的文件
		Properties pps = new Properties();
		try {
			pps.load(rc.getInputStream());
			@SuppressWarnings("unchecked")
			Enumeration<String> propers = (Enumeration<String>) pps.propertyNames();
			while (propers.hasMoreElements()) {
				String key = propers.nextElement();
				log.info("key=" + key + ", value=" + pps.getProperty(key));
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		ctx.close();
	}

	public static void testFileSystemResource() {
		log.info("----------测试testFileSystemResource函数----------");
		FileSystemXmlApplicationContext ctx = new FileSystemXmlApplicationContext();
		Resource rc = ctx.getResource("classpath:config-local-1.properties");
		Properties pps = new Properties();
		try {
			pps.load(rc.getInputStream());
			@SuppressWarnings("unchecked")
			Enumeration<String> propers = (Enumeration<String>) pps.propertyNames();
			while (propers.hasMoreElements()) {
				String key = propers.nextElement();
				log.info("key=" + key + ", value=" + pps.getProperty(key));
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		ctx.close();
	}

	public static void testServletContextResource() {

	}

	public static void testInputStreamResource() {

	}

	public static void testByteArrayResource() {

	}
}
